'''
Created on 2011/03/20

@author: Administrator
'''
from google.appengine.ext import webapp
from util.models import Field
from util.InitDatabase import InitDatabase
from util.GaeEncoder import GaeEncoder
from util.models import PlayerInfo
from util.models import RecentEvaluation
from util.models import EvaluatedFriends
from util.models import PlayTime
from util.models import LuckyItem
from util.models import QualityTitle
from util.models import QuantityTitle

## remove massive data on GAE models because of GAE has some limitations 
#  At present, get request is available with paramater /remove/*
#
class RemoveAll(webapp.RequestHandler):

    def get(self,requestName) :
        limit = 100
        
        if requestName=="player":
            q=PlayerInfo.gql('LIMIT '+str(limit))
       
        elif requestName=="recentevaluation":
            q=RecentEvaluation.gql('LIMIT '+str(limit))
            
        elif requestName=="qntitle":
            q=QuantityTitle.gql('LIMIT '+str(limit))

        elif requestName=="qltitle":
            q=QualityTitle.gql('LIMIT '+str(limit))

        elif requestName=="field":
            q=Field.gql('LIMIT '+str(limit))

        elif requestName=="evaluatedfriends":
            q=EvaluatedFriends.gql('LIMIT '+str(limit))
            
        self.deleteAll(q[0:min(q.count(),limit)],requestName)
        
    ## recursively call this method till data exists
    #
    #
    def deleteAll(self,list,deleteURL):
        if list==None :
            self.response.headers['Content-Type'] = 'text/plain'
            self.response.out.write( 'Error.' )
            return 

        if len(list)<1:
            self.response.headers['Content-Type'] = 'text/plain'
            self.response.out.write( 'OK remove all data.' )
            return 

        for c in list:
            c.delete()

        self.response.headers['Content-Type'] = 'text/html'
        self.response.out.write( '<html>' )
        self.response.out.write( '<head>' )
        self.response.out.write( '<META HTTP-EQUIV="REFRESH" CONTENT="10;URL=/remove/'+deleteURL+'">')
        self.response.out.write( '</head>' )
        self.response.out.write( '<body>' )
        self.response.out.write( '<p>' )
        self.response.out.write( len(list) )
        self.response.out.write( '</p>' )
        self.response.out.write( 'Again after 10seconds.' )
        self.response.out.write( '</body>' )
        self.response.out.write( '</html>' )

## Initialize all data on GAE models, this is used when the application initialize at once
#
#
class InitializeDBs(webapp.RequestHandler): 
    def get(self,requestName):        
        if requestName=="initall":
            self.initAllDBs()
            
        elif requestName=="initstatic":
            self.initStaticDBs()
    
        elif requestName=="initquality":
            self.initQuality()
    ## initialize all models
    #
    #
    def initializeAllDBs(self):        
        initDB = InitDatabase()
        initDB.deleteField()
        initDB.initField()
        initDB.deleteQualityTitle()
        initDB.initQualityTitle()
        initDB.deleteQuantityTitle()
        initDB.initQuantityTitle()
        initDB.deleteLuckyItem()
        initDB.initLuckyItem() 
        
        initDB.deleteRecentEvaluation()
        initDB.initRecentEvaluation() 
        initDB.deletePlayerInfoDB()
        initDB.initPlayerInfoDB()
        initDB.deleteEvaluatedFriends()
        initDB.initEvaluatedFriends()
        initDB.deletePlayTime()
        self.response.out.write("All DBs are deleted and initialized")

    ## initialize static models which doesn't relate to personal information
    #
    #
    def initStaticDBs(self):
        initDB = InitDatabase()
        initDB.deleteField()
        initDB.initField()
        initDB.deleteQualityTitle()
        initDB.initQualityTitle()
        initDB.deleteQuantityTitle()
        initDB.initQuantityTitle()
        initDB.deleteLuckyItem()
        initDB.initLuckyItem() 
        self.response.out.write("Initialized static DBs")

    def initQuality(self):
        initDB = InitDatabase()
        initDB.deleteQualityTitle()
        initDB.initQualityTitle()
        self.response.out.write("Initialized Quality Title")

## check method for data on GAE
#  GET request is available with paramater /check/*
#
class RequestCheck(webapp.RequestHandler):
    def get(self,requestName):        
        if requestName=="playerinfo":
            self.getPlayerInfo()
            
        elif requestName=="playtime":
            self.getPlayTime()
            
        elif requestName=="recentevaluation":
            self.getRecentEvaluation()
            
        elif requestName=="evaluatedfriends":
            self.getEvaluatedFriends()
            
        elif requestName=="field":
            self.getField()
            
        elif requestName=="titles":
            self.getTitles()
            
        elif requestName=="luckyitem":
            self.getLuckyItem()
            
        elif requestName=="all":
            self.getPlayerAllInfo()
            
    def getRecentEvaluation(self):
        g_list = RecentEvaluation.gql('ORDER BY evaluaterID')
        resultJson = GaeEncoder().encode(g_list) 
        self.response.content_type = "application/json"
        self.response.out.write(resultJson)
        
    def getEvaluatedFriends(self):
        g_list = EvaluatedFriends.gql('ORDER BY evaluaterID')
        resultJson = GaeEncoder().encode(g_list) 
        self.response.content_type = "application/json"
        self.response.out.write(resultJson)
         
    def getPlayerInfo(self): 
        g_list = PlayerInfo.gql('ORDER BY playerID')
        resultJson = GaeEncoder().encode(g_list) 
        self.response.content_type = "application/json"
        self.response.out.write(resultJson)
  
    def getPlayTime(self): 
        g_list = PlayerInfo.gql('ORDER BY playerID')
        resultJson = GaeEncoder().encode(g_list) 
        self.response.content_type = "application/json"
        self.response.out.write(resultJson)      
 
    def getLuckyItem(self):
        g_list = LuckyItem.gql('ORDER BY itemID')
        resultJson = GaeEncoder().encode(g_list) 
        self.response.content_type = "application/json"
        self.response.out.write(resultJson)
             
    def getField(self):
        g_list = Field.gql('ORDER BY fieldID')
        resultJson = GaeEncoder().encode(g_list) 
        self.response.content_type = "application/json"
        self.response.out.write(resultJson)
     
    def getTitles(self):
        qlTitles = QualityTitle.gql('ORDER BY titleID')
        resultJson1 = GaeEncoder().encode(qlTitles)
        qnTitles = QuantityTitle.gql('ORDER BY titleID')
        resultJson2 = GaeEncoder().encode(qnTitles)
        self.response.content_type = "application/json"
        self.response.out.write(resultJson1+resultJson2)
        

    def getPlayerAllInfo(self):
        
        g_list = PlayerInfo.gql('ORDER BY playerID')
        resultJson1 = GaeEncoder().encode(g_list) 
       
        g_list = RecentEvaluation.gql('ORDER BY evaluatedID')
        resultJson2 = GaeEncoder().encode(g_list) 
        
        g_list = PlayTime.gql('ORDER BY playerID')
        resultJson3= GaeEncoder().encode(g_list) 
       
        self.response.content_type = "application/json"
        self.response.out.write("*** PlayerInfo"+resultJson1+" *** RecentEvaluation:"+resultJson2+"*** PlayerINfo" + resultJson3)
    


